﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.Configuration;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;



namespace DotNetNuke.Tests.UI.WatiN.AdministratorTests
{
    [TestFixture]

    [DependsOn(typeof(AdminExtensionsTests))]
    public class AdminLanguagesTests : ATBase
    {
        #region Test Setup and TearDown

        public bool LanguagePackInstalled = false;
        public bool PortalAdded = false;

        [SetUp]
        public void SetUp()
        {

            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            if (LanguagePackInstalled)
            {
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                LoginPage loginPage = new LoginPage(homePage);
                if (!homePage.RegisterLink.Text.Contains(TestUsers.Host.DisplayName))
                {
                    if (!homePage.RegisterLink.Text.Equals("Register"))
                    {
                        loginPage.LogoffUser();

                    }
                    loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                }
                //Uninstall the language pack and delete the files
                ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
                ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
                //extensionsPage.ExtensionPackageSelectList.Select("Core Language Pack");
                //System.Threading.Thread.Sleep(1500);
                //extensionsPage.CoreLanguagePackSectionLink.Click();
                System.Threading.Thread.Sleep(2000);
                extensionsPage.GetExtensionUninstallButton(LocaleName, extensionsPage.CoreLanguagePacksTable).Click();
                System.Threading.Thread.Sleep(2000);
                extensionsPage.DeleteExtensionFilesCheckbox.Checked = true;
                //ConfirmDialogHandler dialog = new ConfirmDialogHandler();
                //using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
                //{
                //    extensionsPage.UninstallPackageLink.ClickNoWait();
                //    dialog.WaitUntilExists();
                //    dialog.OKButton.Click();
                //}
                extensionsPage.UninstallPackageLink.ClickNoWait();
                extensionsPage.YesConfirmationButton.Click();
                ieInstance.WaitForComplete();
                LanguagePackInstalled = false;
            }

            if (PortalAdded)
            {
                ieInstance.GoTo(SiteURL + "/logoff.aspx");
                //Remove the portal Folders
                Directory.Delete(PhysicalPath + @"\mytestportal", true);
                Directory.Delete(PhysicalPath + @"\Portals\1", true);
                PortalAdded = false;
            }

            TearDownTest();
        }

        #endregion

        #region Tests

        [Test]
        public void Add_New_Language_Pack_Via_Extensions()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            LanguagePage languagePage = new LanguagePage(homePage);

            //Act 
            extensionsPage.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            ieInstance.WaitForComplete();
            languagePage.GetCheckBoxForLanguage(EnglishLocaleName, "Enabled").Checked = true;
            System.Threading.Thread.Sleep(1000);
            Span flagSpan = languagePage.GetLanguageFlagSpanByLocaleName(LocaleName);

            //Assert
            Assert.IsTrue(flagSpan.Exists);
            Assert.IsTrue(ieInstance.Image(Find.ByAlt(LanguageCode)).Exists);
            Assert.IsFalse(flagSpan.ClassName.Equals("Language Selected"));
            
        }

        [Test]
        [DependsOn("Add_New_Language_Pack_Via_Extensions")]
        public void Edit_Resource_File_For_Site()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Add A language to the site
            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            LanguagePage languagePage = new LanguagePage(homePage);
            extensionsPage.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            ieInstance.WaitForComplete();
            languagePage.GetCheckBoxForLanguage(EnglishLocaleName, "Enabled").Checked = true;
            System.Threading.Thread.Sleep(1000);

            //Change the admins preferred locale
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserEditButton(TestUsers.Admin.DisplayName).Click();
            UserProfilePage profilePage = new UserProfilePage(homePage);
            profilePage.ManageProfileLink.Click();
            profilePage.PreferredLocaleSelectList.SelectByValue(LanguageCode);
            profilePage.ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
           
            //Add A portal
            ieInstance.GoTo(SiteURL + "/Host/Portals.aspx");
            HostPortalPage portalPage = new HostPortalPage(homePage);
            portalPage.AddNewPortalLink.Click();
            portalPage.ChildRadioButton.Click();
            System.Threading.Thread.Sleep(3000);
            portalPage.PortalAliasField.Value = ("localhost/" + ApplicationName + "/MyTestPortal");
            portalPage.PortalTitle.Value = "My Test Portal";
            portalPage.PortalTemplateSelect.Select("Default Website");
            System.Threading.Thread.Sleep(3000);
            portalPage.AdminUserNameField.Value = TestUsers.Admin2.UserName;
            portalPage.AdminFirstNameField.Value = TestUsers.Admin2.FirstName;
            portalPage.AdminLastNameField.Value = TestUsers.Admin2.LastName;
            portalPage.AdminEmailField.Value = TestUsers.Admin2.Email;
            portalPage.AdminPasswordField.Value = TestUsers.Admin2.Password;
            portalPage.AdminConfirmField.Value = TestUsers.Admin2.Password;
            portalPage.CreatePortalLink.Click();
            PortalAdded = true;

            //Act 
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            System.Threading.Thread.Sleep(1500);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            System.Threading.Thread.Sleep(2000);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            languagePage.GetSiteLanguageEditButton(EnglishLocaleName).Click();
            System.Threading.Thread.Sleep(3000);
            languagePage.AdminStringTextField.Value = "New Admin Value";
            languagePage.SaveResourceFileLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            ieInstance.Refresh();
            ieInstance.ClearCache();

            //Assert 
            Span flagSpan = languagePage.GetLanguageFlagSpanByLocaleName(LocaleName);
            flagSpan.ClickNoWait();
            //Span adminMenuItem = ieInstance.Span(Find.ByText("New Admin Value"));
            SiteMenu menu = new SiteMenu(homePage);
            //Assert.IsTrue(adminMenuItem.Exists);
            Assert.IsTrue(menu.BreadCrumbSpan.Text.Contains("New Admin Value"), menu.BreadCrumbSpan.Text);
            menu.HomeButton.Click();
            
            //Log in as the host 
            loginPage.LogoffUser();
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            System.Threading.Thread.Sleep(2000);
            ieInstance.GoTo(SiteURL + "/MyTestPortal/Admin.aspx");
            //adminMenuItem = ieInstance.Span(Find.ByText("New Admin Value"));
            //Assert.IsFalse(adminMenuItem.Exists);
            menu = new SiteMenu(homePage);
            //Assert.IsTrue(menu.MenuAdminSpan.Exists);
            Assert.IsTrue(menu.BreadCrumbLink.Text.Contains("Admin"), menu.BreadCrumbLink.Text);


            //Clean up portal folders and resource file
            string[] files = Directory.GetFiles(PhysicalPath + @"\App_GlobalResources");
            string resourceFilePath = null;
            foreach(string f in files)
            {
                if (f.Contains("Portal"))
                {
                    resourceFilePath = f;
                    break;
                }
                continue;
            }
            if (resourceFilePath != null)
            {
                File.Delete(resourceFilePath);
            }
        }

        [Test]
        [DependsOn("Add_New_Language_Pack_Via_Extensions")]
        public void Edit_Resource_File_For_Host()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            System.Threading.Thread.Sleep(1500);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Add A language to the site
            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            LanguagePage languagePage = new LanguagePage(homePage);
            extensionsPage.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            ieInstance.WaitForComplete();
            //languagePage.GetCheckBoxForLanguage(EnglishLocaleName, "Enabled").Checked = true;
            System.Threading.Thread.Sleep(1000);

            //Change the admins preferred locale
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserEditButton(TestUsers.Admin.DisplayName).Click();
            UserProfilePage profilePage = new UserProfilePage(homePage);
            profilePage.ManageProfileLink.ClickNoWait();
            profilePage.PreferredLocaleSelectList.SelectByValue(LanguageCode);
            profilePage.ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);

            //Add A portal
            ieInstance.GoTo(SiteURL + "/Host/Portals.aspx");
            HostPortalPage portalPage = new HostPortalPage(homePage);
            portalPage.AddNewPortalLink.Click();
            portalPage.ChildRadioButton.Click();
            System.Threading.Thread.Sleep(3000);
            portalPage.PortalAliasField.Value = ("localhost/" + ApplicationName + "/MyTestPortal");
            portalPage.PortalTitle.Value = "My Test Portal";
            portalPage.PortalTemplateSelect.Select("Default Website");
            System.Threading.Thread.Sleep(3000);
            portalPage.AdminUserNameField.Value = TestUsers.Admin2.UserName;
            portalPage.AdminFirstNameField.Value = TestUsers.Admin2.FirstName;
            portalPage.AdminLastNameField.Value = TestUsers.Admin2.LastName;
            portalPage.AdminEmailField.Value = TestUsers.Admin2.Email;
            portalPage.AdminPasswordField.Value = TestUsers.Admin2.Password;
            portalPage.AdminConfirmField.Value = TestUsers.Admin2.Password;
            portalPage.CreatePortalLink.ClickNoWait();
            //portalPage.MessageLink.Click();
            PortalAdded = true;

            //Change the child portal admins locale name
            System.Threading.Thread.Sleep(5000);
            SiteMenu menu = new SiteMenu(homePage);
            ieInstance.GoTo(SiteURL + "/MyTestPortal/Admin/UserAccounts.aspx");
            userPage.GetUserEditButton(TestUsers.Admin2.FirstName + " " + TestUsers.Admin2.LastName).Click();
            profilePage.ManageProfileLink.Click();
            profilePage.PreferredLocaleSelectList.SelectByValue(LanguageCode);
            profilePage.ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);           

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            languagePage.GetHostLanguageEditButton(EnglishLocaleName).Click();
            System.Threading.Thread.Sleep(3000);
            languagePage.AdminStringTextField.Value = "New Admin Value";
            languagePage.SaveResourceFileLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            System.Threading.Thread.Sleep(1500);
            Image languageFlagLink = ieInstance.Span(Find.ByTitle(LocaleName)).Image(Find.Any);

            //Assert 
            ieInstance.GoTo(SiteURL);
            System.Threading.Thread.Sleep(3500);
            loginPage.LogoffUser();
            System.Threading.Thread.Sleep(5000);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            languageFlagLink.Click();
            System.Threading.Thread.Sleep(1500);
            //Span adminMenuItem = ieInstance.Span(Find.ByText("New Admin Value"));
            homePage.AdminConsoleLink.Click();
            //Assert.IsTrue(adminMenuItem.Exists);
            System.Threading.Thread.Sleep(1500);
            Assert.IsTrue(menu.BreadCrumbSpan.Text.Contains("New Admin Value"));
            ieInstance.GoTo(SiteURL + "/MyTestPortal");

            loginPage.LoginLink.ClickNoWait();
            System.Threading.Thread.Sleep(5000);
            loginPage.UserName.Value = TestUsers.Admin2.UserName;
            loginPage.Password.Value = TestUsers.Admin2.Password;
            loginPage.LoginButton.ClickNoWait();
            System.Threading.Thread.Sleep(2500);

            languageFlagLink.Click();
            System.Threading.Thread.Sleep(1500);
            homePage.AdminConsoleLink.Click();
            Assert.IsTrue(menu.BreadCrumbLink.Text.Contains("New Admin Value"));
            ieInstance.GoTo(SiteURL + "/MyTestPortal");
            //adminMenuItem = ieInstance.Span(Find.ByText("New Admin Value"));
            //Assert.IsTrue(adminMenuItem.Exists);
            //Clean up portal folders and resource file
            string[] files = Directory.GetFiles(PhysicalPath + @"\App_GlobalResources");
            string resourceFilePath = null;
            foreach (string f in files)
            {
                if (f.Contains("Host"))
                {
                    resourceFilePath = f;
                    break;
                }
                else continue;
            }
            File.Delete(resourceFilePath);
        }

        [Test]
        [DependsOn("Add_New_Language_Pack_Via_Extensions")]
        public void Edit_Resource_File_For_System()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Add A language to the site
            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            LanguagePage languagePage = new LanguagePage(homePage);
            extensionsPage.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            System.Threading.Thread.Sleep(1500);
            languagePage.GetCheckBoxForLanguage(EnglishLocaleName, "Enabled").Checked = true;
            System.Threading.Thread.Sleep(1000);

            //Change the admins preferred locale
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserEditButton(TestUsers.Admin.DisplayName).Click();
            UserProfilePage profilePage = new UserProfilePage(homePage);
            profilePage.ManageProfileLink.Click();
            profilePage.PreferredLocaleSelectList.SelectByValue(LanguageCode);
            profilePage.ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);

            //Add A portal
            ieInstance.GoTo(SiteURL + "/Host/Portals.aspx");
            HostPortalPage portalPage = new HostPortalPage(homePage);
            portalPage.AddNewPortalLink.Click();
            portalPage.ChildRadioButton.Click();
            System.Threading.Thread.Sleep(3000);
            portalPage.PortalAliasField.Value = ("localhost/" + ApplicationName + "/MyTestPortal");
            portalPage.PortalTitle.Value = "My Test Portal";
            portalPage.PortalTemplateSelect.Select("Default Website");
            System.Threading.Thread.Sleep(3000);
            portalPage.AdminUserNameField.Value = TestUsers.Admin2.UserName;
            portalPage.AdminFirstNameField.Value = TestUsers.Admin2.FirstName;
            portalPage.AdminLastNameField.Value = TestUsers.Admin2.LastName;
            portalPage.AdminEmailField.Value = TestUsers.Admin2.Email;
            portalPage.AdminPasswordField.Value = TestUsers.Admin2.Password;
            portalPage.AdminConfirmField.Value = TestUsers.Admin2.Password;
            portalPage.CreatePortalLink.Click();
            portalPage.MessageLink.Click();
            PortalAdded = true; 

            //Change the child portal admins locale name
            System.Threading.Thread.Sleep(1500);
            ieInstance.GoTo(SiteURL + "/MyTestPortal/Admin/UserAccounts.aspx");
            System.Threading.Thread.Sleep(1500);
            userPage.GetUserEditButton(TestUsers.Admin2.FirstName + " " + TestUsers.Admin2.LastName).Click();
            profilePage.ManageProfileLink.ClickNoWait();
            profilePage.PreferredLocaleSelectList.Select(LocaleName);
            profilePage.ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);           

            //Act 
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            languagePage.GetSystemLanguageEditButton(EnglishLocaleName).Click();
            System.Threading.Thread.Sleep(3000);
            languagePage.AdminStringTextField.Value = "New Admin Value";
            languagePage.SaveResourceFileLink.Click();
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");

            //Assert 
            loginPage.LogoffUser();
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            Span adminMenuItem = ieInstance.Span(Find.ByText("New Admin Value"));
            Assert.IsTrue(adminMenuItem.Exists);
            ieInstance.GoTo(SiteURL + "/MyTestPortal");

            loginPage.LoginLink.Click();
            loginPage.UserName.Value = TestUsers.Admin2.UserName;
            loginPage.Password.Value = TestUsers.Admin2.Password;
            loginPage.LoginButton.Click();

            adminMenuItem = ieInstance.Span(Find.ByText("New Admin Value"));
            Assert.IsTrue(adminMenuItem.Exists);

            //Clean up portal folders and resource file
            string[] files = Directory.GetFiles(PhysicalPath + @"\App_GlobalResources");
            string resourceFilePath = null;
            Match match;
            string globalResourceFileName = @"globalresources\.\w\w\-\w\w\.resx";
            foreach (string f in files)
            {
                match = Regex.Match(f, globalResourceFileName);
                if (match.Success)
                {
                    resourceFilePath = f;
                    break;
                }
                else continue;
            }
            File.Delete(resourceFilePath);  
        }

        [Test]
        [DependsOn("Edit_Resource_File_For_Site")]
        public void Delete_Resource_File_For_Site()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Add A language to the site
            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            LanguagePage languagePage = new LanguagePage(homePage);
            extensionsPage.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            ieInstance.WaitForComplete();
            languagePage.GetCheckBoxForLanguage(EnglishLocaleName, "Enabled").Checked = true;
            System.Threading.Thread.Sleep(1000);

            //Change the admins preferred locale
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserEditButton(TestUsers.Admin.DisplayName).Click();
            UserProfilePage profilePage = new UserProfilePage(homePage);
            profilePage.ManageProfileLink.Click();
            profilePage.PreferredLocaleSelectList.SelectByValue(LanguageCode);
            profilePage.ProfileUpdateLink.Click();
            System.Threading.Thread.Sleep(1500);

            //Edit the Resource File
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            System.Threading.Thread.Sleep(2000);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            languagePage.GetSiteLanguageEditButton(EnglishLocaleName).Click();
            System.Threading.Thread.Sleep(3000);
            languagePage.AdminStringTextField.Value = "New Admin Value";
            languagePage.SaveResourceFileLink.Click();
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");

            //Act 
            languagePage.GetSiteLanguageEditButton(EnglishLocaleName).Click();
            ConfirmDialogHandler dialog = new ConfirmDialogHandler(); 
            using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
            {
                languagePage.DeleteResourceFileLink.ClickNoWait();
                dialog.WaitUntilExists();
                dialog.OKButton.Click();
            }

            //Assert 
            Assert.IsTrue(languagePage.ModuleMessageSpan.InnerHtml.Contains("Portal"));
            Assert.IsTrue(languagePage.ModuleMessageSpan.InnerHtml.Contains("was correctly deleted"));
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            Span adminMenuItem = ieInstance.Span(Find.ByText("New Admin Value"));
            Assert.IsFalse(adminMenuItem.Exists);
            SiteMenu menu = new SiteMenu(homePage);
            Assert.IsFalse(menu.BreadCrumbSpan.Text.Contains("New Admin Value"));
        }


        [Test]
        [DependsOn("Edit_Resource_File_For_Host")]
        public void Delete_Resource_File_For_Host()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Add A language to the site
            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            LanguagePage languagePage = new LanguagePage(homePage);
            extensionsPage.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            System.Threading.Thread.Sleep(1500);
            languagePage.GetCheckBoxForLanguage(EnglishLocaleName, "Enabled").Checked = true;
            System.Threading.Thread.Sleep(1000);

            //Change the admins preferred locale
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserEditButton(TestUsers.Admin.DisplayName).Click();
            UserProfilePage profilePage = new UserProfilePage(homePage);
            profilePage.ManageProfileLink.Click();
            System.Threading.Thread.Sleep(1500);
            profilePage.PreferredLocaleSelectList.SelectByValue(LanguageCode);
            profilePage.ProfileUpdateLink.Click();
            System.Threading.Thread.Sleep(1500);

            //Add A portal
            ieInstance.GoTo(SiteURL + "/Host/Portals.aspx");
            HostPortalPage portalPage = new HostPortalPage(homePage);
            portalPage.AddNewPortalLink.Click();
            portalPage.ChildRadioButton.Click();
            System.Threading.Thread.Sleep(3000);
            portalPage.PortalAliasField.Value = ("localhost/" + ApplicationName + "/MyTestPortal");
            portalPage.PortalTitle.Value = "My Test Portal";
            portalPage.PortalTemplateSelect.Select("Default Website");
            System.Threading.Thread.Sleep(3000);
            portalPage.AdminUserNameField.Value = TestUsers.Admin2.UserName;
            portalPage.AdminFirstNameField.Value = TestUsers.Admin2.FirstName;
            portalPage.AdminLastNameField.Value = TestUsers.Admin2.LastName;
            portalPage.AdminEmailField.Value = TestUsers.Admin2.Email;
            portalPage.AdminPasswordField.Value = TestUsers.Admin2.Password;
            portalPage.AdminConfirmField.Value = TestUsers.Admin2.Password;
            portalPage.CreatePortalLink.Click();
            PortalAdded = true;

            //Edit the Resource File
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            languagePage.GetHostLanguageEditButton(EnglishLocaleName).Click();
            System.Threading.Thread.Sleep(3000);
            languagePage.AdminStringTextField.Value = "New Admin Value";
            languagePage.SaveResourceFileLink.Click();
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");

            //Act 
            languagePage.GetHostLanguageEditButton(EnglishLocaleName).Click();
            ConfirmDialogHandler dialog = new ConfirmDialogHandler();
            using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
            {
                languagePage.DeleteResourceFileLink.ClickNoWait();
                dialog.WaitUntilExists();
                dialog.OKButton.Click();
            }

            //Assert 
            Assert.IsTrue(languagePage.PopUpMessageSpan.InnerHtml.Contains("Host"));
            Assert.IsTrue(languagePage.PopUpMessageSpan.InnerHtml.Contains("was correctly deleted"));
            languagePage.ReturnLink.Click();
            System.Threading.Thread.Sleep(1500);
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            System.Threading.Thread.Sleep(3500);
            loginPage.LogoffUser();
            System.Threading.Thread.Sleep(1500);
            ieInstance.ClearCache();
            ieInstance.Refresh();
            System.Threading.Thread.Sleep(1500);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            Span adminMenuItem = homePage.RBAdminMenuDiv.Span(Find.ByText("New Admin Value"));
            Assert.IsFalse(adminMenuItem.Exists);
            ieInstance.GoTo(SiteURL + "/MyTestPortal");
            System.Threading.Thread.Sleep(2500);

            loginPage.LoginLink.Click();
            loginPage.UserName.Value = TestUsers.Admin2.UserName;
            loginPage.Password.Value = TestUsers.Admin2.Password;
            loginPage.LoginButton.Click();
            adminMenuItem = homePage.RBAdminMenuDiv.Span(Find.ByText("New Admin Value"));
            Assert.IsFalse(adminMenuItem.Exists);
        }

        [Test]
        [DependsOn("Edit_Resource_File_For_System")]
        public void Delete_Resource_File_For_System()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Add A language to the site
            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            LanguagePage languagePage = new LanguagePage(homePage);
            extensionsPage.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            ieInstance.WaitForComplete();
            languagePage.GetCheckBoxForLanguage(EnglishLocaleName, "Enabled").Checked = true;
            System.Threading.Thread.Sleep(1000);

            //Change the admins preferred locale
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            System.Threading.Thread.Sleep(1000);
            userPage.GetUserEditButton(TestUsers.Admin.DisplayName).Click();
            UserProfilePage profilePage = new UserProfilePage(homePage);
            profilePage.ManageProfileLink.ClickNoWait();
            profilePage.PreferredLocaleSelectList.SelectByValue(LanguageCode);
            profilePage.ProfileUpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);

            //Add A portal
            ieInstance.GoTo(SiteURL + "/Host/Portals.aspx");
            HostPortalPage portalPage = new HostPortalPage(homePage);
            portalPage.AddNewPortalLink.Click();
            portalPage.ChildRadioButton.Click();
            System.Threading.Thread.Sleep(3000);
            portalPage.PortalAliasField.Value = ("localhost/" + ApplicationName + "/MyTestPortal");
            portalPage.PortalTitle.Value = "My Test Portal";
            portalPage.PortalTemplateSelect.Select("Default Website");
            System.Threading.Thread.Sleep(3000);
            portalPage.AdminUserNameField.Value = TestUsers.Admin2.UserName;
            portalPage.AdminFirstNameField.Value = TestUsers.Admin2.FirstName;
            portalPage.AdminLastNameField.Value = TestUsers.Admin2.LastName;
            portalPage.AdminEmailField.Value = TestUsers.Admin2.Email;
            portalPage.AdminPasswordField.Value = TestUsers.Admin2.Password;
            portalPage.AdminConfirmField.Value = TestUsers.Admin2.Password;
            portalPage.CreatePortalLink.Click();
            PortalAdded = true;

            //Edit the Resource File
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            languagePage.GetSystemLanguageEditButton(EnglishLocaleName).Click();
            System.Threading.Thread.Sleep(3000);
            languagePage.AdminStringTextField.Value = "New Admin Value";
            languagePage.SaveResourceFileLink.Click();
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");

            //Act 
            languagePage.GetSystemLanguageEditButton(EnglishLocaleName).Click();
            ConfirmDialogHandler dialog = new ConfirmDialogHandler();
            using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
            {
                languagePage.DeleteResourceFileLink.ClickNoWait();
                dialog.WaitUntilExists();
                dialog.OKButton.Click();
            }

            //Assert
            Assert.IsTrue(languagePage.ModuleMessageSpan.InnerHtml.Contains("was correctly deleted"));
            loginPage.LogoffUser();
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            Span adminMenuItem = ieInstance.Span(Find.ByText("New Admin Value"));
            Assert.IsFalse(adminMenuItem.Exists);
            SiteMenu menu = new SiteMenu(homePage);
            Assert.IsFalse(menu.BreadCrumbSpan.Text.Contains("New Admin Value"));
            ieInstance.GoTo(SiteURL + "/MyTestPortal");

            System.Threading.Thread.Sleep(1000);
            loginPage.LoginLink.Click();
            loginPage.UserName.Value = TestUsers.Admin2.UserName;
            loginPage.Password.Value = TestUsers.Admin2.Password;
            loginPage.LoginButton.Click();
            adminMenuItem = ieInstance.Span(Find.ByText("New Admin Value"));
            Assert.IsFalse(adminMenuItem.Exists);
        }

        [Test]
        [DependsOn("Add_New_Language_For_The_Site")]
        public void Create_Core_Language_Pack()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);

            //Add a language
            languagePage.AddNewLanguage(LocaleName, "English (United States)");
            System.Threading.Thread.Sleep(2500);
           
            //Edit the resource file
            languagePage.GetHostLanguageEditButton(EnglishLocaleName).Click();
            System.Threading.Thread.Sleep(2000);
            languagePage.AdminStringTextField.Value = "New Admin Value";
            languagePage.SaveResourceFileLink.Click();
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");

            //Act 
            languagePage.CreateLanguagePackLink.Click();
            languagePage.LanguagePackLocaleSelectList.Select(LocaleName);
            languagePage.SelectLanguagePackType("Core");
            languagePage.FileNameField.Value = "CoreTest";
            languagePage.CreateLink.Click();
            System.Threading.Thread.Sleep(1200);

            //Assert
            Assert.IsTrue(languagePage.PopUpMessageSpan.InnerHtml.Contains("The Language Pack(s) were created"));

            //Assert that a language pack was created and delete the file
            string[] files = Directory.GetFiles(PhysicalPath + @"\Install\Language");
            string resourceFilePath = null;
            Match match;
            bool resourcePackFound = false;
            string globalResourceFileName = @"ResourcePack\.CoreTest\.\d\.\d\.\d\.\w\w\-\w\w\.zip";
            foreach (string f in files)
            {
                match = Regex.Match(f, globalResourceFileName);
                if (match.Success)
                {
                    resourceFilePath = f;
                    resourcePackFound = true;
                    break;
                }
                else continue;
            }
            Assert.IsTrue(resourcePackFound);
            File.Delete(resourceFilePath);
        }

        [Test]
        public void Create_Module_Language_Pack()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);

            //Act 
            languagePage.CreateLanguagePackLink.Click();
            languagePage.LanguagePackLocaleSelectList.Select("English (United States)");
            languagePage.SelectLanguagePackType("Module");
            System.Threading.Thread.Sleep(3000);
            languagePage.SelectModuleCheckbox("HTML");
            languagePage.CreateLink.Click();
          
            //Assert
            Assert.IsTrue(languagePage.PopUpMessageSpan.InnerHtml.Contains("The Language Pack(s) were created"));

            //Assert that a language pack was created and delete the file
            string[] files = Directory.GetFiles(PhysicalPath + @"\Install\Language");
            string resourceFilePath = null;
            Match match;
            bool resourcePackFound = false;
            string globalResourceFileName = @"ResourcePack\.DNN_HTML\.\d\.\d\.\d\.\w\w\-\w\w\.zip";
            foreach (string f in files)
            {
                match = Regex.Match(f, globalResourceFileName);
                if (match.Success)
                {
                    resourceFilePath = f;
                    resourcePackFound = true;
                    break;
                }
                else continue;
            }
            Assert.IsTrue(resourcePackFound);
            File.Delete(resourceFilePath);
        }

        [Test]
        public void Create_Provider_Language_Pack()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);

            //Act 
            languagePage.CreateLanguagePackLink.ClickNoWait();
            languagePage.LanguagePackLocaleSelectList.Select("English (United States)");
            languagePage.SelectLanguagePackType("Provider");
            System.Threading.Thread.Sleep(2000);
            languagePage.SelectModuleCheckbox("ASP.NET Membership Provider");
            languagePage.CreateLink.ClickNoWait();

            //Assert
            Assert.IsTrue(languagePage.PopUpMessageSpan.InnerHtml.Contains("The Language Pack(s) were created"), languagePage.PopUpMessageSpan.Text);

            //Assert that a language pack was created and delete the file
            string[] files = Directory.GetFiles(PhysicalPath + @"\Install\Language");
            string resourceFilePath = null;
            Match match;
            bool resourcePackFound = false;
            string globalResourceFileName = @"ResourcePack\.DotNetNuke\.TelerikEditorProvider\.\d\.\d\.\d\.\w\w\-\w\w\.zip";
            foreach (string f in files)
            {
                match = Regex.Match(f, globalResourceFileName);
                if (match.Success)
                {
                    resourceFilePath = f;
                    resourcePackFound = true;
                    break;
                }
                else continue;
            }
            Assert.IsTrue(resourcePackFound);
            //File.Delete(resourceFilePath);
        }

        [Test]
        public void Create_Full_Language_Pack()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);

            //Act 
            languagePage.CreateLanguagePackLink.Click();
            languagePage.LanguagePackLocaleSelectList.Select("English (United States)");
            System.Threading.Thread.Sleep(1000);
            languagePage.SelectLanguagePackType("Full");
            System.Threading.Thread.Sleep(3000);
            languagePage.FileNameField.Value = "FullTest";
            languagePage.CreateLink.Click();

            //Assert
            Assert.IsTrue(languagePage.PopUpMessageSpan.InnerHtml.Contains("The Language Pack(s) were created"));

            //Assert that a language pack was created and delete the file
            string[] files = Directory.GetFiles(PhysicalPath + @"\Install\Language");
            string resourceFilePath = null;
            Match match;
            bool resourcePackFound = false;
            string globalResourceFileName = @"ResourcePack\.FullTest\.\d\.\d\.\d\.\w\w\-\w\w\.zip";
            foreach (string f in files)
            {
                match = Regex.Match(f, globalResourceFileName);
                if (match.Success)
                {
                    resourceFilePath = f;
                    resourcePackFound = true;
                    break;
                }
                else continue;
            }
            Assert.IsTrue(resourcePackFound);
            File.Delete(resourceFilePath);
        }

        [Test]
        public void Edit_Time_Zone()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);

            //Add a new language
            languagePage.AddNewLanguage(LocaleName, "English (United States)");
            System.Threading.Thread.Sleep(500);

            //Act 
            languagePage.TimeZoneEditorLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            languagePage.LocaleSelectList.Select(LocaleName);
            languagePage.DublinTextField.Value = "(UTC  00:00) Test Name";
            languagePage.UpdateLink.Click();
            System.Threading.Thread.Sleep(1500);

            //Assert 
            Image altLanguageImage = ieInstance.Image(Find.ByAlt(LanguageCode));
            homePage.RegisterLink.Click();
            UserProfilePage profilePage = new UserProfilePage(homePage);
            profilePage.EditProfileLink.Click();
            System.Threading.Thread.Sleep(1000);
            altLanguageImage.Click();
            System.Threading.Thread.Sleep(1500);
           
            Assert.IsTrue(profilePage.TimeZoneSelectList.Option("(UTC 00:00) Test Name").Exists);
            Assert.IsFalse(profilePage.TimeZoneSelectList.Option("(UTC 00:00) Dublin, Edinburgh, Lisbon, London").Exists);

            //Delete the time zone file
            string[] files = Directory.GetFiles(PhysicalPath + @"\App_GlobalResources");
            string resourceFilePath = null;
            Match match;
            string globalResourceFileName = @"TimeZones\.\w\w\-\w\w\.xml";
            foreach (string f in files)
            {
                match = Regex.Match(f, globalResourceFileName);
                if (match.Success)
                {
                    resourceFilePath = f;
                    break;
                }
                else continue;
            }
            File.Delete(resourceFilePath);
        }

        [Test]
        public void Add_New_Language_For_The_Site()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);

            //Act
            languagePage.AddNewLanguage(LocaleName, "English (United States)");
            System.Threading.Thread.Sleep(1500);
            Span flagSpan = languagePage.GetLanguageFlagSpanByLocaleName(LocaleName);

            //Assert
            Assert.IsTrue(flagSpan.Exists);
            Assert.IsTrue(ieInstance.Image(Find.ByAlt(LanguageCode)).Exists);
            Assert.IsFalse(flagSpan.ClassName.Equals("Language Selected"));
        }

        [Test]
        public void Change_Portal_Default_Language()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);

            //Add a language to the site
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            
            extensionsPage.InstallExtension(LanguagePath);
                      

            //Login as the admin
            loginPage.LogoffUser();
            System.Threading.Thread.Sleep(1500);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);

            //Add a user to the site
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            UserPage userPage = new UserPage(homePage);
            userPage.AddUser(TestUsers.User1.UserName, TestUsers.User1.Password, TestUsers.User1.FirstName, TestUsers.User1.LastName, TestUsers.User1.EmailAddress, TestUsers.User1.DisplayName);
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/UserAccounts.aspx");
            System.Threading.Thread.Sleep(1500);
            userPage.AllDisplayLink.Click();
            System.Threading.Thread.Sleep(1000);

            //Change the new users preferred locale
            userPage.GetUserEditButton(TestUsers.User1.DisplayName).ClickNoWait();
            UserProfilePage profilePage = new UserProfilePage(homePage);
            System.Threading.Thread.Sleep(1500);
            profilePage.ManageProfileLink.Click();
            System.Threading.Thread.Sleep(2500);
            profilePage.PreferredLocaleSelectList.SelectByValue("en-US");
            profilePage.ProfileUpdateLink.ClickNoWait();

            //Act
            System.Threading.Thread.Sleep(2000);
            ieInstance.GoTo(SiteURL + "/Admin/languages.aspx");
            LanguagePage languagePage = new LanguagePage(homePage);
            System.Threading.Thread.Sleep(1500);
            languagePage.SelectAndUpdateNewDefaultLanguage(LocaleName, "rcbItem ");
            System.Threading.Thread.Sleep(1000);

            //Register a user
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            loginPage.LogoffUser();
            System.Threading.Thread.Sleep(1500);
            userPage.RegisterUser(TestUsers.Register.UserName, TestUsers.Register.Password, TestUsers.Register.FirstName, TestUsers.Register.LastName, TestUsers.Register.EmailAddress, TestUsers.Register.DisplayName);

            //Assert
            System.Threading.Thread.Sleep(1000);
            homePage.RegisterLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            profilePage.EditProfileLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            profilePage.ManageProfileLink.Click();
            Assert.IsTrue(profilePage.PreferredLocaleSelectList.SelectedItem.Equals(LocaleName), profilePage.PreferredLocaleSelectList.SelectedItem);
            loginPage.LogoffUser();
            loginPage.LoginUser(TestUsers.User1.UserName, TestUsers.User1.Password);
            System.Threading.Thread.Sleep(1000);
            homePage.RegisterLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            profilePage.EditProfileLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            profilePage.ManageProfileLink.Click();
            Assert.IsTrue(profilePage.PreferredLocaleSelectList.SelectedItem.Equals("English (United States)"));
        }

        #endregion
    }
}